﻿<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
<script type="text/javascript" src="../lib/html5.js"></script>
<script type="text/javascript" src="../lib/respond.min.js"></script>
<script type="text/javascript" src="../lib/PIE_IE678.js"></script>
<![endif]-->
    <link href="../lib/layui/css/layui.css" rel="stylesheet" type="text/css" />
    <link href="../css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link href="../css/H-ui.admin.css" rel="stylesheet" type="text/css" />
    <link href="../lib/Hui-iconfont/1.0.1/iconfont.css" rel="stylesheet" type="text/css" />
    <!--[if IE 6]>
<script type="text/javascript" src="../lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
    <title>采摘出库管理</title>
</head>
<body>
    <div class="pd-5">
        <!--   <div id="tab-category" class="HuiTab">
            <div class="tabBar cl" id="tab-categorybar">
                <span>
                    <label id="lb_worktypeshow">作业基本信息</label></span><span>作业图片信息</span><span>作业用料信息</span>
            </div>
            <div class="tabCon" id="divtab1">
                12
            </div>
            <div class="tabCon" id="divtab2">
                23
            </div>
        </div>-->
        <div class="row cl">
            <div class="formControls col-5">
                <span class="l">
                    <a href="javascript:;" onclick="hcrainOpenDiv('采摘入库明细','sctaskoutstore_inLogList.html')" class="btn btn-danger   round"><i class="Hui-iconfont">&#xe687;</i> 管理入库明细</a>
                    <a href="javascript:;" onclick="hcrainOpenDiv('原料库存明细','sctaskYlstoreList.html')" class="btn btn-danger   round"><i class="Hui-iconfont">&#xe687;</i> 管理原料库存明细</a>
                </span>
            </div>
        </div>
        <div class="row cl" id="findfdiv">
            <div class="formControls col-2"></div>
            <div class="formControls col-8">
                <input type="text" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" placeholder="入库时间" id="findinfodt1_times" name="findinfodt1_times" class="input-text Wdate" style="width: 120px;">
                <input type="text" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" placeholder="入库时间" id="findinfodt1_timee" name="findinfodt1_timee" class="input-text Wdate" style="width: 120px;">
                <input type="text" class="input-text" placeholder="输入农作物名称" id="findfv1" name="findfv1" style="width: 120px;">

                <input type="text" class="input-text" placeholder="地块" id="finddcinfo" name="finddcinfo" style="width: 120px;">
            </div>
            <!--     <div class="formControls col-3">
                <span class="select-box">
                    <select class="select" size="1" name="findinfofv4" id="findinfofv4">
                        <option value="待入库">待出库</option>
                        <option value="出库中">出库中</option>
                        <option value="已出库">已出库</option>
                        <option value="全部">全部</option>
                    </select></span>
            </div>-->
            <div class="formControls col-2">
                <button onclick="hcrainpage()" class="btn btn-success radius" id="" name=""><i class="Hui-iconfont">&#xe665;</i> 查询</button>
            </div>
        </div>
        <div class="cl pd-5 bg-1 bk-gray mt-5 ">
            <div class="form form-horizontal">
                <div class="row cl" style="margin-top: 0px">
                    <div class="formControls col-2">
                        <span class="select-box">
                            <select class="select" size="1" name="finouttype" id="finouttype">
                                <option value="自加工出库">农场加工处理</option>
                              <option value="发往加工中心">发往深圳加工厂</option> 
                                <option value="调整仓库">调整仓库</option>
                                <option value="其它">其它</option>
                            </select>
                        </span>
                    </div>
                    <div class="formControls col-2">
                        <input type="text" class="input-text" placeholder="出库说明" id="foutdesc" name="foutdesc">
                    </div>
                 
                    <div class="formControls col-1">
                        <span class="select-box">
                            <select class="select" size="1" name="fcarid" id="fcarid">
                            </select>
                        </span>
                    </div>
                    <label class="form-label col-1">交接人：</label>
                    <div class="formControls col-2 layui-form">
                        <select name="inoutusernamesel" id='inoutusernamesel' lay-filter="testselect" lay-verify="required" lay-search="">
                            <option value="">直接选择或搜索选择</option>
                        </select>
                    </div>
                     <label class="form-label col-1">发货日期：</label>
                    <div class="formControls col-2">
                        <input type="text" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" name="testdataaddtime" id="testdataaddtime" class="input-text Wdate" />
                    </div>
                    <div class="formControls col-1"><a href="javascript:;" onclick="savetotaskstre()" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 提交</a></div>
                </div>
            </div>
        </div>
        <div id="hcvueapp" class="mt-5 ml-20">
            <div class="panel panel-default" v-for="site in listinfo">
                <div class="panel-header" style="margin: 0px; padding: 0px;">{{ site.fv1 }}--总量：{{ site.storeall }}</div>
                <div class="panel-body" style="margin: 0px; padding: 0px;">
                    <table class="table table-border table-bordered table-hover" style="margin: 0px; padding: 0px;">
                        <thead>
                            <tr>
                                <th>任务信息</th>
                                <th>状态</th>
                                <th>入库量</th>
                                <th>已出库</th>
                                <th>出库量</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="worklist in site.subinfo" v-bind:class="{ success: (worklist.hasoutall<=worklist.storeall), 'warning':worklist.hasoutall>=worklist.storeall }">
                                <td>{{ worklist.taskno}}-地块：{{ worklist.dkname}}-入库：{{ worklist.dataaddtime.Value}}-采摘：{{ worklist.taskworktime.Value}}</td>
                                <td>{{ worklist.outstate }}-{{ worklist.productstate }}</td>
                                <td>{{ worklist.storeall }}</td>
                                <td>{{ worklist.hasoutall }}</td>
                                <td>
                                    <input type="text" class="input-text" placeholder="出库量" v-model="worklist.needtoout" onkeyup="this.value=this.value.replace(/[^0-9-.]+/,'');" />
                                    <!-- <div v-if="worklist.infofv4 === '待入库'">
                                        <input type="text" class="input-text" placeholder="实际入库量" v-model="worklist.infonv3" onkeyup="this.value=this.value.replace(/[^0-9-]+/,'');" />
                                    </div>
                                    <div v-else>
                                        <input type="text" class="input-text" placeholder="实际入库量" v-model="worklist.infonv3" onkeyup="this.value=this.value.replace(/[^0-9-]+/,'');" />
                                    </div>-->
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="mt-5 row">
                <div id="page1"></div>
            </div>

        </div>
    </div>

    <script type="text/javascript" src="../lib/layui/layui2.js"></script>
    <script type="text/javascript" src="../js/vue.min.js"></script>
    <script>
        var hcvueapp = new Vue({
            el: '#hcvueapp',
            data: {
                listinfo: []
            },
            methods: {

            }
        })
    </script>
    <script src="../hcjsui/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="../lib/layui/layui.js"></script>
    <script type="text/javascript" src="../lib/layer/1.9.3/layer.js"></script>
    <script type="text/javascript" src="../lib/laypage/1.2/laypage.js"></script>
    <script type="text/javascript" src="../lib/My97DatePicker/WdatePicker.js"></script>
    <script src="../hcjsui/js/_Define.js" type="text/javascript"></script>
    <script type="text/javascript" src="../js/hcui.js"></script>
    <script type="text/javascript" src="../js/hcui_list.js"></script>

    <script type="text/javascript">
        var uiform = null;
        var whereaddinfo = "";
        $("#findinfodt1_times").val((getDateByChange(new Date(), 0)).Format("yyyy-MM-dd"));
        $("#testdataaddtime").val((getDateByChange(new Date(), 0)).Format("yyyy-MM-dd"));//开始时间
        $(function () {
            spsetBaseNoBt("yw_baseinfo", "id", "yw_baseinfo as T", "*", "#tablelist", "sctaskoutstore", "生产任务管理", "sctaskoutstore");
            hcdoselbyPost("id,fv1 as name", "yw_baseinfo", "type='16' and  maintype='16' and databranchid=" + HCUser.HCUorg, "", "", "id",
                   function (data) {
                       SetSelectWithArrayIdName("#fcarid", data);
                   });
            layui.use(['form'], function () {
                uiform = layui.form();
                //人员下拉加载 
                hcdoselbyPost("empid  as id,concat(depname,'-',empname) as name,empname as username,mobile as  tel,depname as address", "view_user", "branchid='" + HCUser.HCUorg + "'", "", "", "id",
                    function (data) {
                        SetSelectWithArrayIdName("#inoutusernamesel", data);
                        uiform.render();
                    });
            });

            hcrainpage(1);
            //$.Huitab("#tab-category .tabBar span", "#tab-category .tabCon", "current", "click", "0");
            //$.Huifold("#Huifold2 .item h4", "#Huifold2 .item .info", "", 3, "click"); /*5个参数顺序不可打乱，分别是：相应区,隐藏显示的内容,速度,类型,事件 mouseover click */
        });
        //出库处理
        function savetotaskstre() {
            if ($("#finouttype").val() == "发往加工中心")
            {
                layer.alert("原料的发货，已调整到‘农场发货登记’中处理。不能在当前页处理。“选择采收日期-发货日期 输入发货信息提交即可。”", { icon: 2 });
                return;
            }
            if ($("#testdataaddtime").val()) {

            }
            else {
                layer.msg("发货/处理日期，不能为空", { icon: 2 });
                return;
            }
            var needupdate = 0;
            var message = "";//"<input id='ssssssssss'></input>";
            var postworklist = [];
            for (var sp in hcvueapp.listinfo) {
                var subinfo = hcvueapp.listinfo[sp].subinfo;
                message += "<br/> " + hcvueapp.listinfo[sp].fv1 + "[";
                for (var lindex in subinfo) {
                    if (subinfo[lindex].needtoout == null || subinfo[lindex].needtoout == ""
                        || subinfo[lindex].needtoout == " " || subinfo[lindex].needtoout <= 0
                        || isNaN(subinfo[lindex].needtoout)) {
                        continue;
                    }
                    needupdate++;
                    message += "(" + subinfo[lindex].needtoout + ")";
                    postworklist.push({ "id": subinfo[lindex].id, "number": subinfo[lindex].needtoout, "info": subinfo[lindex].productname });
                }
                message += "]";
            }
            layer.confirm('确认信息：' + '<br/>发货/处理日期：' + $("#testdataaddtime").val() + '<br/>记录数：' + needupdate + message, {
                btn: [$("#finouttype").val(), '取消'] //按钮
            }, function () {
                //是否库存足够
                hcui_ajaxasync = false;
                hcapipost("../api/Hcrain/PostCheckStoreOutOk", postworklist, function (data) {
                    if (data != "ok") {
                        layer.msg("提交失败 " + data, { icon: 2 });
                    }
                    else {
                        savetotaskstreDo();
                    }
                });
            }, function () {
            });
        }
        function savetotaskstreDo() {
            var outguid = NewGuid();
            var needupdate = 0;
            var updatecount = 0;

            for (var sp in hcvueapp.listinfo) {
                var subinfo = hcvueapp.listinfo[sp].subinfo;
                for (var lindex in subinfo) {
                    if (subinfo[lindex].needtoout == null || subinfo[lindex].needtoout == ""
                        || subinfo[lindex].needtoout == " " || subinfo[lindex].needtoout <= 0
                        || isNaN(subinfo[lindex].needtoout)) {
                        continue;
                    }
                    hcui_ajaxasync = false;
                    //信息通用部分
                    var addinfo = "outgid%" + outguid
                        //+ "$outstate%运输中"
                        //+ "$inoutstorestate%待入库"
                        + "$inouttype%" + $("#finouttype").val()
                        + "$outdesc%" + $("#foutdesc").val()
                        + "$dkid%" + subinfo[lindex].dkid
                        + "$dkname%" + subinfo[lindex].dkname
                        + "$taskid%" + subinfo[lindex].taskid
                        + "$taskno%" + subinfo[lindex].taskno
                        + "$tasklistid%" + subinfo[lindex].tasklistid
                        + "$storehouseid%0$hasoutall%0"//默认初始化
                        + "$productid%" + hcvueapp.listinfo[sp].id
                        + "$productname%" + hcvueapp.listinfo[sp].fv1
                        + "$storeall%" + subinfo[lindex].needtoout
                        + "$taskfromorgid%" + subinfo[lindex].taskfromorgid
                        + "$taskfromorgname%" + subinfo[lindex].taskfromorgname
                        //+ "$datafromtable%hsc_task_store"
                        + "$taskworktime%" + subinfo[lindex].taskworktime.Value//采摘时间
                        + "$datafromkeyid%" + subinfo[lindex].id
                        + "$productstate%" + subinfo[lindex].productstate
                        + "$csgetpc%" + subinfo[lindex].csgetpc
                        + "$jjuserid%" + $("#inoutusernamesel").val()
                        + "$jjusername%" + $("#inoutusernamesel").find("option:selected").text();

                    //+ "$datafromdesc%出库";

                    var addinfototable = "hsc_task_store_out";
                    if ($("#finouttype").val() == "自加工出库") {
                        addinfototable = "hsc_task_process_work";
                        //自加工出库=>加工记录表
                        //信息发货部分
                        addinfo = addinfo
                           + "$outstate%待加工"
                           + "$inoutstorestate%待入库"
                           + "$datafromtable%hsc_task_store"
                           + "$datafromdesc%取原料";
                        var isNeedUpdate = false;
                        //如果之前已出过库到 自加工，则更新旧记录数量  不再新插入 
                        hcdoselbyPost("id", "hsc_task_process_work", "taskno ='" + subinfo[lindex].taskno + "' and  DATE_FORMAT(dataaddtime,'%Y-%m-%d')= DATE_FORMAT(NOW(),'%Y-%m-%d') and datafromtable='hsc_task_store' and databranchid=" + HCUser.HCUorg, "", "", "id",
                            function (data) {
                                if (data.length > 0) {
                                    isNeedUpdate = true;
                                    hcdoedit("hsc_task_process_work", "id", data[0].id, "storeall%hcrainself(storeall+" + subinfo[lindex].needtoout + ")", function (info) {
                                        console.log(info);
                                        if (info == '更新成功') {
                                            updatecount++;
                                            hcdoedit("hsc_task_store", "id", subinfo[lindex].id, "outstate%出库中$hasoutall%hcrainself(hasoutall+" + subinfo[lindex].needtoout + ")", function (info2) {
                                            });

                                        }
                                    });
                                }
                            });
                        if (isNeedUpdate) continue;
                    }
                    else {
                        addinfototable = "hsc_task_store_out";
                        //发货处理（发到加工中心，调整，其它）=>运输表
                        //信息发货部分
                        addinfo = addinfo
                            + "$outstate%运输中"
                            + "$inoutstorestate%待入库"
                            + "$datafromtable%hsc_task_store"
                            + "$datafromdesc%出库" + "$carid%" + $("#fcarid").val()
                        + "$carinfo%" + $("#fcarid").find("option:selected").text();

                    }
                    addinfo = addinfo + "$dataaddtime%" + $("#testdataaddtime").val();//重指时间
                    //console.log(addinfo);
                    //出库处理-这里要禁用异步
                    hcdoadd2017(addinfototable, addinfo, 2, "", function (info) {
                        //更新记录--已出库
                        if (info == "ok") {
                            updatecount++;
                            hcdoedit("hsc_task_store", "id", subinfo[lindex].id, "outstate%出库中$hasoutall%hcrainself(hasoutall+" + subinfo[lindex].needtoout + ")", function (info2) {
                            });

                           

                        }
                        else { layer.msg('处理过程有异常：' + info, { icon: 2 }); }
                    });
                    //hcui_ajaxasync = true;
                }
            }

            hcapigetNoError("../api/Task/GetReSetOutInfoToCenter", { "databranchid": HCUser.HCUorg, "day": $("#testdataaddtime").val() }, function (backdata) {
            });

            layer.msg('完成操作，记录数：' + updatecount, { icon: 1 });
            //重新加载 
            if (updatecount > 0) {
                hcrainpage(1);
            }


        }
        function hcrainpage(curr) {
            var suminfowhere1 = "";
            var finddcno = $("#findfv1").val();
            if (finddcno) {
                whereaddinfo = " and fv1 like '%" + finddcno + "%'";
            }
            else { whereaddinfo = ""; }
            var suminfowhere = "";
            var finddcinfo = $("#finddcinfo").val();
            if (finddcinfo) {
                suminfowhere = "dkname-||like||" + finddcinfo + ",";
            }
            else { suminfowhere = ""; }

            if ($("#findinfodt1_times").val()) {
                suminfowhere1 += " and taskworktime>='" + $("#findinfodt1_times").val() + " 00:00:00'";
                suminfowhere += "taskworktime-||>=||" + $("#findinfodt1_times").val() + " 00:00:00,";
            }
            if ($("#findinfodt1_timee").val()) {
                suminfowhere1 += " and taskworktime<='" + $("#findinfodt1_timee").val() + " 23:59:59'";
                suminfowhere += "taskworktime-||<=||" + $("#findinfodt1_timee").val() + " 23:59:59,";
            }

            //$.ajax({
            //    type: "post",
            //    url: "../ashx/spui.ashx",
            //    data: {
            //        "action": "getpageinfo",
            //        "pagenow": curr || 1,
            //        "pagesize": 50,
            //        "select": "t.id,t.fv1,t.fv2,0 as storeall",
            //        "table": "yw_baseinfo as t",
            //        "group": "",
            //        "having": "",
            //        "where": " type=1 and maintype=1 " + whereaddinfo + " and id in(select productid from hsc_task_store where   databranchid=" + HCUser.HCUorg + " and storehouseid=0 " + suminfowhere1 + " and storeall>hasoutall )",//有库存的
            //        "order": "",
            //        "subinfo": "id,taskfromorgid,taskfromorgname,taskid,taskno,tasklistid,productname,dkid,dkname,dataaddtime,storeall,storeall-hasoutall as needtoout,lockcout,hasoutall,outstate,taskworktime,productstate#hsc_task_store#productid-@id,databranchid-" + HCUser.HCUorg + "," + suminfowhere + "storehouseid-0,storeall-||>||@@hasoutall#id#"//子查询   当前物品+当前机构+库存大于0
            //    },
            //    async: true,
            //    cache: false,
            //    error: function (error) {

            //    },
            //    success: function (data) {
            //        var result = JSON.parse(data);
            //        hcvueapp.listinfo = result.data;
            //        //if (hcvueapp.listinfo == null || hcvueapp.listinfo.length == 0) {
            //        //    result.message = "没有数据"; /// / / layer.msg('没有相关数据噢', { icon: 5 });
            //        //}
            //        //计算总量
            //        hcvueapp.listinfo.forEach(function (e) {
            //            e.subinfo.forEach(function (list) {
            //                e.storeall = (e.storeall * 1 + (list.storeall * 1 - list.hasoutall * 1)).toFixed(2);
            //            });
            //        });
            //        //显示分页
            //        laypage({
            //            cont: 'page1', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
            //            pages: result.message || 0, //通过后台拿到的总页数
            //            curr: curr || 1, //当前页
            //            jump: function (obj, first) { //触发分页后的回调
            //                if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
            //                    hcrainpage(obj.curr);
            //                }
            //            }
            //        });

            //    }
            //});


            $.ajax({
                type: "post",
                url: "../ashx/spui.ashx",
                data: {
                    "action": "getpageinfo",
                    "pagenow": curr || 1,
                    "pagesize": 50,
                    "select": "t.id,t.fv1,t.fv2,0 as storeall",
                    "table": "yw_baseinfo as t inner join (select DISTINCT productid from hsc_task_store where   databranchid=" + HCUser.HCUorg + " and storehouseid=0 " + suminfowhere1 + " and storeall>(hasoutall+yloutall) )  as t3",
                    "group": "",
                    "having": "",
                    "where": " t.type=1 and t.maintype=1 " + whereaddinfo + " and t.id =t3.productid ",//有库存的
                    "order": "",
                    "subinfo": "csgetpc,id,taskfromorgid,taskfromorgname,taskid,taskno,tasklistid,productname,dkid,dkname,dataaddtime,storeall,storeall-hasoutall-yloutall as needtoout,lockcout,yloutall+hasoutall as hasoutall,outstate,taskworktime,productstate#hsc_task_store#productid-@id,databranchid-" + HCUser.HCUorg + "," + suminfowhere + "storehouseid-0,storeall-||>||@@(hasoutall+yloutall)#id#"//子查询   当前物品+当前机构+库存大于0
                },
                async: true,
                cache: false,
                error: function (error) {

                },
                success: function (data) {
                    var result = JSON.parse(data);
                    hcvueapp.listinfo = result.data;
                    //if (hcvueapp.listinfo == null || hcvueapp.listinfo.length == 0) {
                    //    result.message = "没有数据"; /// / / layer.msg('没有相关数据噢', { icon: 5 });
                    //}
                    //计算总量
                    hcvueapp.listinfo.forEach(function (e) {
                        e.subinfo.forEach(function (list) {
                            e.storeall = (e.storeall * 1 + (list.storeall * 1 - list.hasoutall * 1)).toFixed(2);
                        });
                    });
                    //显示分页
                    laypage({
                        cont: 'page1', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                        pages: result.message || 0, //通过后台拿到的总页数
                        curr: curr || 1, //当前页
                        jump: function (obj, first) { //触发分页后的回调
                            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                                hcrainpage(obj.curr);
                            }
                        }
                    });

                }
            });



        };

        //重新加载-重载
        function findreolad(isback) {
            hcrainpage(1);
        }

        function getDateByChange(date, days) {
            if (days == undefined || days == '') {
                days = 1;
            }
            var date = new Date(date);
            date.setHours(date.getHours() - days);
            return date;

        }

    </script>
</body>
</html>
